package com.example.sportplay.controller;

import com.alibaba.fastjson.JSON;
import com.example.sportplay.bean.User;
import com.example.sportplay.dao.UserDao;
import com.fasterxml.jackson.annotation.JsonAlias;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

// 我们整个controller的方法都使用字符串, @RestController
@RestController
public class LoginController {

    @Autowired  // 自动注入进来
    UserDao userDao;

    @CrossOrigin    // 解决后端跨域问题, post提交不支持这种方式
    @RequestMapping("/test")    // 声明一个路由
    public String test() {
        return "test";
    }

    @CrossOrigin
    @RequestMapping("/login")
    public String login(@RequestBody User user) {
        //初始信息
        String flag = "error";
        User us = userDao.getUserByMassage(user.getUsername(), user.getPassword());
        // map
        HashMap<String, Object> res = new HashMap<>();
        if(us != null) {
            flag = "ok";
        }
        res.put("flag", flag);
        res.put("user", user);
        // 首先需要在pom.xml引入json, 才能调用
        // 把一个对象转化成json字符串
        String res_json = JSON.toJSONString(res);
        return res_json;
    }
}
